package com.utilmodule;

import android.content.Context;
import android.graphics.Bitmap;
import android.os.Environment;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.lang.ref.SoftReference;
import java.util.HashMap;

/**
 * 缓存
 * 
 * @author liuhongyuan
 * 
 */
public class CacheUtil {
	public static String rootPath;
	public static String cacheBasePath;
	public static String cacheSessionPath;
	public static String cacheDownLoadPath;
	public static String cacheLongtermPath = rootPath + "cache/longterm/";
	public static String remoteImageCache = rootPath
			+ "cache/session/remote-image-cache";
	public static HashMap<String, SoftReference<Bitmap>> imageCache;

	public static String getSDPath() {
		File sdDir = null;
		boolean sdCardExist = Environment.getExternalStorageState().equals(
				Environment.MEDIA_MOUNTED); // 判断sd卡是否存在
		if (sdCardExist) {
			sdDir = Environment.getExternalStorageDirectory();// 获取跟目录
		}
		return sdDir.toString();
	}

	/*
	 *@author liuhongyuan
	 *@date 2017/4/28 10:55
	 *@description 获取文件大小
	 */
	public static String formatFileSize(long length) {
		String result = null;
		int sub_string = 0;
		if (length >= 1073741824) {
			sub_string = String.valueOf((float) length / 1073741824).indexOf(
					".");
			result = ((float) length / 1073741824 + "000").substring(0,
					sub_string + 3) + "GB";
		} else if (length >= 1048576) {
			sub_string = String.valueOf((float) length / 1048576).indexOf(".");
			result = ((float) length / 1048576 + "000").substring(0,
					sub_string + 3) + "MB";
		} else if (length >= 1024) {
			sub_string = String.valueOf((float) length / 1024).indexOf(".");
			result = ((float) length / 1024 + "000").substring(0,
					sub_string + 3) + "KB";
		} else if (length < 1024)
			result = Long.toString(length) + "B";
		return result;
	}

	public static void createCacheDir(Context context) {
		if (context.getExternalCacheDir() == null) {
			rootPath = context.getFilesDir().getPath();
		}else {
			rootPath = context.getExternalCacheDir().getPath();
		}
		File rootF = new File(rootPath);
		cacheBasePath = rootPath + "/";
		cacheSessionPath = rootPath + "/session/";
		cacheDownLoadPath = rootPath + "/download/";
		// cacheLongtermPath = rootPath + "cache/longterm/";
		if (!rootF.exists()) {
			rootF.mkdir();
		}
		File f = new File(cacheBasePath);
		if (!f.exists()) {
			f.mkdir();
		}
		File fSession = new File(cacheSessionPath);
		if (!fSession.exists()) {
			fSession.mkdir();
		}
		File fdownload = new File(cacheDownLoadPath);
		if (!fdownload.exists()) {
			fdownload.mkdir();
		}
	}

	public static void createCacheDir(String dir) {
		File f = new File(cacheBasePath + dir);
		if (!f.exists()) {
			f.mkdir();
		}
	}

	public static void createCacheSessionDir(String dir) {
		File f = new File(cacheSessionPath + dir);
		if (!f.exists()) {
			f.mkdir();
		}
	}

	public static void createCacheLongtermDir(String dir) {
		File f = new File(cacheLongtermPath + dir);
		if (!f.exists()) {
			f.mkdir();
		}
	}

	/*
	 *@author liuhongyuan
	 *@date 2017/4/28 10:54
	 *@description 删除session data
	 */
	public static void cleanSessionCache() {
		try {
			del(cacheSessionPath);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	/*
	 *@author liuhongyuan
	 *@date 2017/4/28 11:06
	 *@description 删除download data
	 */
	public static void cleanDownLoadCache() {
		try {
			del(cacheDownLoadPath);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	/*
	 *@author liuhongyuan
	 *@date 2017/4/28 11:06
	 *@description 删除所有数据
	 */
	public static void cleanAllSdCache() {
		try {
			del(cacheBasePath);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	/*
	 *@author liuhongyuan
	 *@date 2017/4/28 10:54
	 *@description 删除文件
	 */
	public static void del(String filepath) throws IOException {
		try {
			File f = new File(filepath);
			if (f.exists() && f.isDirectory()) {
				if (f.listFiles().length == 0) {
					f.delete();

				} else {
					File delFile[] = f.listFiles();
					int i = f.listFiles().length;
					for (int j = 0; j < i; j++) {
						if (delFile[j].isDirectory()) {
							del(delFile[j].getAbsolutePath());
						}
						delFile[j].delete();
					}
				}
			} else {
				f.delete();
			}
		} catch (Exception e) {

		}
	}

	/*
	 *@author liuhongyuan
	 *@date 2017/4/28 10:54
	 *@description 获取base data
	 */
	public static Object getBaseData(String fileName) {
		Object readob;
		try {
			if (new File(fileName = cacheBasePath + fileName).exists()) {
				FileInputStream fi = new FileInputStream(fileName);
				BufferedInputStream bi = new BufferedInputStream(fi, 10000);
				ObjectInputStream oi = new ObjectInputStream(bi);
				readob = oi.readObject();
				oi.close();
				bi.close();
				fi.close();
			} else {
				return null;
			}
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
		return readob;
	}

	/*
	 *@author liuhongyuan
	 *@date 2017/4/28 10:53
	 *@description 获取session data
	 */
	public static Object getSessionData(String fileName) {
		Object readob;
		try {
			if (new File(fileName = cacheSessionPath + fileName).exists()) {
				FileInputStream fi = new FileInputStream(fileName);
				BufferedInputStream bi = new BufferedInputStream(fi, 10000);
				ObjectInputStream oi = new ObjectInputStream(bi);
				readob = oi.readObject();
				oi.close();
				bi.close();
				fi.close();
			} else {
				return null;
			}
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
		return readob;
	}

	/*
	 *@author liuhongyuan
	 *@date 2017/4/28 10:53
	 *@description 获取download data
	 */
	public static Object getDownLoadData(String fileName) {
		Object readob;
		try {
			if (new File(fileName = cacheDownLoadPath + fileName).exists()) {
				FileInputStream fi = new FileInputStream(fileName);
				BufferedInputStream bi = new BufferedInputStream(fi, 10000);
				ObjectInputStream oi = new ObjectInputStream(bi);
				readob = oi.readObject();
				oi.close();
				bi.close();
				fi.close();
			} else {
				return null;
			}
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
		return readob;
	}

	/*
	 *@author liuhongyuan
	 *@date 2017/4/28 10:53
	 *@description 存base data
	 */
	public static void cacheBaseData(String fileName, Object object) {
		if (fileName != null && fileName.length() > 0) {
			try {
				fileName = cacheBasePath + fileName;
				FileOutputStream fo = new FileOutputStream(fileName);
				ObjectOutputStream oo = new ObjectOutputStream(
						new BufferedOutputStream(fo));
				oo.writeObject(object);
				oo.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

	/*
	 *@author liuhongyuan
	 *@date 2017/4/28 10:49
	 *@description 存session data
	 */
	public static void cacheSessionData(String fileName, Object object) {
		if (fileName != null && fileName.length() > 0) {
			try {
				fileName = cacheSessionPath + fileName;
				FileOutputStream fo = new FileOutputStream(fileName);
				ObjectOutputStream oo = new ObjectOutputStream(
						new BufferedOutputStream(fo));
				oo.writeObject(object);
				oo.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

	/*
	 *@author liuhongyuan
	 *@date 2017/4/28 10:49
	 *@description 存download data
	 */
	public static void cacheDownLoadData(String fileName, Object object) {
		if (fileName != null && fileName.length() > 0) {
			try {
				fileName = cacheDownLoadPath + fileName;
				FileOutputStream fo = new FileOutputStream(fileName);
				ObjectOutputStream oo = new ObjectOutputStream(
						new BufferedOutputStream(fo));
				oo.writeObject(object);
				oo.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

	public static void init() {
		imageCache = new HashMap<String, SoftReference<Bitmap>>();
	}

	public static boolean checkBaseData(String fileName) {
		if (fileName == null || fileName.length() == 0) {
			return false;
		}
		fileName = cacheBasePath + fileName;
		File f = new File(fileName);
		if (f.exists()) {
			return true;
		} else {
			return false;
		}
	}

	/*
	 *@author liuhongyuan
	 *@date 2017/4/28 10:51
	 *@description 检查session data
	 */
	public static boolean checkSessionData(String fileName) {
		if (fileName == null || fileName.length() == 0) {
			return false;
		}
		fileName = cacheSessionPath + fileName;
		File f = new File(fileName);
		if (f.exists()) {
			return true;
		} else {
			return false;
		}
	}

	/*
	 *@author liuhongyuan
	 *@date 2017/4/28 10:51
	 *@description 检查download data
	 */
	public static boolean checkDownLoadData(String fileName) {
		if (fileName == null || fileName.length() == 0) {
			return false;
		}
		fileName = cacheDownLoadPath + fileName;
		File f = new File(fileName);
		if (f.exists()) {
			return true;
		} else {
			return false;
		}
	}
}
